'''
Created on 2012-3-26

@author: Wang_ZJ
'''
def init_env(debug, base_path, logfile, dburi, cacheuri):
    import global_vars
    global_vars.g_debug = debug
    global_vars.g_base_path = base_path
    # database configuration
    from sqlalchemy import create_engine, MetaData
    from sqlalchemy.orm import scoped_session, sessionmaker
    global_vars.g_dburi = dburi
    global_vars.g_dbengine = create_engine(dburi, convert_unicode=True, encoding="utf-8", pool_recycle=3600*7, echo=False, connect_args={'reconnect':True},)
    global_vars.g_dbmeta = MetaData(bind=global_vars.g_dbengine)
    global_vars.g_dbsession = scoped_session(sessionmaker(global_vars.g_dbengine))
    
    import logging.handlers
    logging.getLogger().setLevel(logging.DEBUG) # override default of WARNING
    logfile = logging.handlers.TimedRotatingFileHandler(logfile , 'midnight')
    logfile.setLevel(logging.DEBUG)
    logfile.setFormatter(logging.Formatter('%(asctime) s %(levelname)-8s %(module)s: %(message)s'))
    logging.getLogger().addHandler(logfile)
    global_vars.g_logger = logging.getLogger()
    
    import memcache
    global_vars.g_cache = memcache.Client([cacheuri],debug=global_vars.g_debug)

    global_vars.g_logger.critical('server initial completed, g_debug = %r, g_dburi = %s', global_vars.g_debug, global_vars.g_dburi)
